<template>
  <div class="category">
    <template v-if="!data_category || !categoryTitle">
      <div class="loading">
        <svg
          t="1616053408617"
          class="icon"
          viewBox="0 0 1024 1024"
          version="1.1"
          xmlns="http://www.w3.org/2000/svg"
          p-id="17643"
          width="64"
          height="64"
        >
          <path
            d="M739.487 798.497v-78.751c0-86.667-49.501-164.599-124.671-202.725 75.171-38.137 124.671-116.076 124.671-202.735v-78.747c34.78-1.811 62.499-30.658 62.499-65.863 0-36.359-29.582-65.941-65.972-65.941H287.953c-36.358 0-65.941 29.581-65.941 65.941 0 35.217 27.738 64.072 62.505 65.866v78.743c0 86.661 49.495 164.592 124.666 202.735-75.171 38.127-124.666 116.059-124.666 202.726v78.748c-34.768 1.793-62.505 30.646-62.505 65.868 0 36.368 29.582 65.95 65.941 65.95h448.061c36.39 0 65.972-29.581 65.972-65.95 0-35.21-27.719-64.054-62.499-65.865zM256.513 169.678c0-17.337 14.117-31.44 31.44-31.44h448.061c17.353 0 31.472 14.102 31.472 31.44 0 17.332-14.119 31.45-31.472 31.45H287.953c-17.322-0.001-31.44-14.118-31.44-31.45z m62.504 550.068c0-86.86 58.626-163.385 142.559-186.056a17.26 17.26 0 0 0 12.734-16.661c0-7.785-5.22-14.626-12.734-16.646-83.933-22.709-142.559-99.227-142.559-186.094v-78.655H704.98v78.655c0 86.867-58.626 163.385-142.559 186.094-7.515 2.021-12.735 8.862-12.735 16.646a17.262 17.262 0 0 0 12.735 16.661c83.933 22.673 142.559 99.196 142.559 186.056v78.659H670c-8.233-28.4-34.42-49.172-65.478-49.172h-75.286V474.897c80.982-8.637 144.278-77.368 144.278-160.603v-17.381H350.466v17.381c0 83.232 63.29 151.961 144.262 160.602v274.337H419.45c-31.053 0-57.241 20.771-65.473 49.172h-34.964v-78.659z m416.998 176.066H287.954c-17.322 0-31.44-14.117-31.44-31.455 0-17.332 14.117-31.45 31.44-31.45h448.061c17.353 0 31.472 14.119 31.472 31.45 0 17.337-14.119 31.455-31.472 31.455z"
            p-id="17644"
            fill="#1296db"
          ></path>
        </svg>
        <span class="loading_text">加载中...</span>
      </div>
    </template>
    <template v-show="data_category">
      <div class="searchBox">
        <van-search
          shape="round"
          background="#fff"
          placeholder="搜索商品信息"
          v-model="searchText"
          @keydown.enter="search(searchText)"
        />
      </div>
      <van-index-bar :index-list="categoryTitle">
        <template v-for="item in data_category">
          <div class="item" :key="item.id">
            <van-index-anchor :index="item.cate_name">
              <template #default>
                ———
                <span style="fontweight: bold">{{ item.cate_name }}</span> ———
              </template>
            </van-index-anchor>
            <div class="child">
              <router-link
                :to="`/goods_list?id=${c.id}&title=${c.cate_name}`"
                v-for="c in item.children"
                :key="c.id"
              >
                <van-image :src="c.pic" />
                <span>{{ c.cate_name }}</span>
              </router-link>
            </div>
          </div>
        </template>
      </van-index-bar>
      <div style="height:48vh;weight:100%"></div>
    </template>
  </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
export default {
  data() {
    return {
      categoryTitle: [],
      searchText: "",
    };
  },
  created() {
    this.axios.get("/api/category").then((response) => {
      this.getCategoryData(response.data.data);
      this.categoryTitle = response.data.data.map((item) => {
        return item.cate_name;
      });
    });
  },
  methods: {
    ...mapMutations(["getCategoryData"]),
    search(data) {
      this.$router.push({
        path: "/goods_list",
        query: {
          s: data,
        },
      });
    },
  },
  computed: {
    ...mapState({
      data_category: (state) => state.data.category,
    }),
  },
};
</script>

<style lang="less">
.category {
  width: 100%;
  background-color: #fff;
  .loading {
    width: 100%;
    height: 100vh;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .loading_text {
      color: #272822;
    }
    @keyframes move {
      0% {
        transform: rotateZ(0deg);
      }

      15% {
        transform: rotateZ(180deg);
      }

      55% {
        transform: rotateZ(180deg);
      }

      70% {
        transform: rotateZ(360deg);
      }

      100% {
        transform: rotateZ(360deg);
      }
    }

    .icon {
      animation: move 4s linear 2s infinite;
    }
  }
  .searchBox {
    width: 100%;
    border-bottom: 1px solid #f6f6f6;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 10;
  }
  .van-index-bar {
    width: 100%;
    margin-bottom: 50px;
    .van-index-bar__sidebar {
      width: 25vw;
      height: calc(100vh - 104px);
      left: 0;
      top: 54px;
      transform: translateY(0px);
      background-color: #f7f7f7;
      .van-index-bar__index {
        padding: 0;
        height: 50px;
        line-height: 50px;
        font-size: 14px;
      }
      .van-index-bar__index--active {
        background-color: #fff;
        border-left: 2px solid #fc5454;
      }
    }
  }
  .item {
    padding-left: 25vw;
    margin-bottom: 10px;
    .van-index-anchor {
      width: 100%;
      height: 55px;
      line-height: 54px;
      text-align: center;
      margin-bottom: 10px;
    }
    .child {
      width: 100%;
      display: flex;
      flex-wrap: wrap;
      a {
        flex: 33.33%;
        text-align: center;
        height: 100px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        margin-bottom: 10px;
        .van-image {
          width: 100%;
          display: flex;
          justify-content: center;
          align-items: center;
          img {
            width: 80%;
          }
        }
        span {
          font-size: 14px;
        }
      }
    }
  }
}
</style>